using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Template : BaseItem
    {
        
        public enum NodeTypes{
            Node = 1, Folder = 2, Page = 3, Img = 4, FLV = 5, Pdf = 6, Flash = 7, Mp3 = 8, Downloadable = 9, Excel = 10, Word = 11, PowerPoint = 12,


        }
        private Template() : base() { }
        private Template(Hashtable table) : base(table) { }
        private Template(Hashtable table, List<Template> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TemplateDal();
        }

        public static Template CreateInstance()
        {
            return new Template();
        }

        private static List<Template> GetItems(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<Template> tmp = new List<Template>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Template(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static List<Template> GetAllBySiteId(int siteId)
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAllBySiteId(siteId));
        }

        public static List<Template> GetAllByParentTemplateId(int parentTemplateId)
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAllByParentTemplateId(parentTemplateId));
        }

        public static List<Template> GetAllByChildTemplateId(int childTemplateId)
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAllByChildTemplateId(childTemplateId));
        }


        public static List<Template> GetAll()
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAll());
        }

        public static Template GetById(int id)
        {
            TemplateDal dal = new TemplateDal();
            Hashtable table = dal.GetById(id);
            if (table != null)
            {
                return new Template(table);
            }
            else
            {
                return null;
            }
        }

        protected override void Load(Hashtable table)
        {
            Id = (int)table[TemplateDal.Fields.Id];
            SiteId = (int)table[TemplateDal.Fields.SiteId];
            Name = (string)table[TemplateDal.Fields.Name];
            NodeType = (NodeTypes)table[TemplateDal.Fields.NodeType];
            Notes = (string)table[TemplateDal.Fields.Notes];
            ExcludeFromSearch = (bool)table[TemplateDal.Fields.ExcludeFromSearch];
            
        }

        public int Id { get; private set; }
        public int SiteId { get; set; }
        public string Name { get; set; }
        public NodeTypes NodeType {get; set;}
        public string Notes { get; set; }
        public bool ExcludeFromSearch { get; set; }
	

        public new void Update()
        {
            base.Update(); 
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int idOut = Id;
            ((TemplateDal)dal).Update(ref idOut, SiteId, Name, (int)NodeType, Notes, ExcludeFromSearch, ref concurencyId);
            Id = idOut;
            return Id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateDal)dal).Delete(Id, concurencyId);
        }

    }
}
